{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculate Returns\n",
    "\n",
    "![returns_chart.png](returns_chart_720_358.png)\n",
    "\n",
    "Using the formula $ \\frac{p_{t} - p_{t-1}}{p_{t-1}} $, let's apply it to some example prices. For this exercise, we'll calculate the returns for each day using the closing price data in `close`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "close = pd.DataFrame(\n",
    "    {\n",
    "        'ABC': [1, 5, 3, 6, 2],\n",
    "        'EFG': [12, 51, 43, 56, 22],\n",
    "        'XYZ': [35, 36, 36, 36, 37],},\n",
    "    pd.date_range('10/01/2018', periods=5, freq='D'))\n",
    "close"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the returns formula on the closing prices for the ticker \"ABC\" should give us `[(5-1)/1, (3-5)/5, (6-3)/3, (2-6)/6]` or `[4, -0.4, 1, -0.66]`. To calculate this for the whole DataFrame, we'll use the [DataFrame.shift](https://pandas.pydata.org/pandas-docs/version/0.21/generated/pandas.DataFrame.shift.html) function.\n",
    "\n",
    "This function allows us to shift the rows of data. For example, the following shifts the rows in `close` two days back."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "close.shift(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The data for the row \"2018-10-03\" contains data that is two days in the past. You'll also notice the \"NaN\" values for \"2018-10-01\" and \"2018-10-02\". Since there's not data two days in the past for these dates, it returns a \"NaN\" value.\n",
    "\n",
    "Use this function, you can also shift in the future using a negative number. Let's shift one day in the future."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "close.shift(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quiz\n",
    "Using what you know about the [DataFrame.shift](https://pandas.pydata.org/pandas-docs/version/0.21/generated/pandas.DataFrame.shift.html) function, implement the function.\n",
    "\n",
    "Once you successfully implemented the quiz, you can can continue to the next concept in the classroom."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import quiz_tests\n",
    "\n",
    "\n",
    "def calculate_returns(close):\n",
    "    \"\"\"\n",
    "    Compute returns for each ticker and date in close.\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    close : DataFrame\n",
    "        Close prices for each ticker and date\n",
    "    \n",
    "    Returns\n",
    "    -------\n",
    "    returns : DataFrame\n",
    "        Returns for each ticker and date\n",
    "    \"\"\"\n",
    "    # TODO: Implement Function\n",
    "    \n",
    "    return None\n",
    "\n",
    "\n",
    "quiz_tests.test_calculate_returns(calculate_returns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quiz Solution\n",
    "If you're having trouble, you can check out the quiz solution [here](calculate_returns_solution.ipynb)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
